package com.kaifamiao.codec.test;

import org.apache.commons.codec.digest.DigestUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CompareFile {

    @Test
    public void test1() throws IOException {
        File f = new File( "D:/uploads/hello.txt" );
        if( f.isFile() ) {
            InputStream in = new FileInputStream(f);
            String sh = DigestUtils.sha1Hex( in );
            System.out.println( sh );
        }
    }

    @Test
    public void test2() throws IOException {
        File f = new File( "D:/uploads/abc.txt" );
        if( f.isFile() ) {
            InputStream in = new FileInputStream(f);
            String sh = DigestUtils.sha1Hex( in );
            System.out.println( sh );
        }
    }

    @Test
    public void test3() throws IOException {
        File f1 = new File( "D:/uploads/hello.txt" );
        File f2 = new File( "D:/uploads/abc.txt" );
        if( f1.isFile() && f2.isFile()) {
            InputStream in = new FileInputStream(f1);
            String sh1 = DigestUtils.sha1Hex( in );
            System.out.println( sh1 );
            in.close();

            in = new FileInputStream(f2);
            String sh2 = DigestUtils.sha1Hex( in );
            in.close();
            System.out.println( sh2 );

            System.out.println( sh1.equals( sh2 ) );
        }
    }

}
